<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>学生管理系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }

        h2 {
            margin-bottom: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
        }

        th {
            background-color: #f2f2f2;
            font-weight: bold;
        }

        tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        form {
            display: inline-block;
        }

        button, input[type="submit"], input[type="reset"] {
            padding: 5px 10px;
            margin-left: 5px;
        }

        label {
            display: inline-block;
            width: 100px;
            text-align: right;
            margin-right: 10px;
        }

        input[type="text"], input[type="number"] {
            padding: 5px;
            width: 200px;
        }
    </style>
</head>
<body>
<h2>查询学生</h2>
<form action="${pageContext.request.contextPath}/students" method="get">
    <button type="submit">查询</button>
</form>

<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>专业</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <c:forEach var="student" items="${students}">
        <tr>
            <td>${student.id}</td>
            <td>${student.name}</td>
            <td>${student.age}</td>
            <td>${student.gender}</td>
            <td>${student.major}</td>
            <td>
                <!-- 修改 -->
                <form action="${pageContext.request.contextPath}/students" method="post">
                    <input type="hidden" name="action" value="update">
                    <input type="hidden" name="id" value="${student.id}">
                    <input type="text" name="name" placeholder="请输入姓名" value="${student.name}">
                    <input type="number" name="age" placeholder="请输入年龄" value="${student.age}">
                    <input type="text" name="gender" placeholder="请输入性别" value="${student.gender}" maxlength="1">
                    <input type="text" name="major" placeholder="请输入专业" value="${student.major}">
                    <button type="submit">更新</button>
                </form>
                <!-- 删除 -->
                <form action="${pageContext.request.contextPath}/students" method="post" onsubmit="return confirm('确定要删除吗')">
                    <input type="hidden" name="action" value="delete">
                    <input type="hidden" name="id" value="${student.id}">
                    <button type="submit">删除</button>
                </form>
            </td>
        </tr>
    </c:forEach>
    </tbody>
</table>

<!-- 新增 -->
<h2>添加新学生</h2>
<form action="${pageContext.request.contextPath}/students" method="post">
    <input type="hidden" name="action" value="add">
    <label for="name">学生姓名</label>
    <input type="text" id="name" name="name" placeholder="请输入姓名">
    <label for="age">学生年龄</label>
    <input type="number" id="age" name="age" placeholder="请输入年龄">
    <label for="gender">学生性别</label>
    <input type="text" id="gender" name="gender" placeholder="请输入性别" maxlength="1">
    <label for="major">学生专业</label>
    <input type="text" id="major" name="major" placeholder="请输入专业">
    <input type="submit" value="提交">
    <input type="reset" value="重置">
</form>
</body>
</html>
